package YAVMM::Server::REST;
use warnings;
use strict;

use Dancer ':syntax';
set serializer => 'JSON';

use Template;

use YAVMM::Server::Utils;
use YAVMM::Server::Auth;

use TryCatch;
use DateTime;

our $VERSION = '0.1';

before sub {
    if (request->path_info !~ m|\A /json/settings/ |x) {
	# ACL check here
    }
};

# get all sections
get '/json/settings/sections' => sub {
};

# get all keys for section
get '/json/settings/:section' => sub {
};

# get a key in a section
get '/json/settings/:section/:key' => sub {
    try {
	my $value = YAVMM::Server::Utils::get_settings->get(
	    params->{section},
	    params->{key}
	    );
	return { params->{section} => { params->{key} => $value } };
    } catch (YAVMM::Error $err) {
	status 500;
	return { status => "Unable to get setting: $err" };
    }
};

# set a key
put '/json/settings/:section/:key' => sub {
    try {
	my $value = YAVMM::Server::Utils::get_settings->set(
	    params->{section},
	    params->{key},
	    params->{value},
	    );
	return {
	    params->{section} => {
		params->{key} => YAVMM::Server::Utils::get_settings->get(
		    params->{section},
		    params->{key},
		    )
	    }
	};
    } catch (YAVMM::Error $err) {
	status 500;
	return { status => "Unable to set setting: $err" };
    }
};

# delete a key in a section
del '/json/settings/:section/:key' => sub {
    try {
	my $value = YAVMM::Server::Utils::get_settings->unset(
	    params->{section},
	    params->{key}
	    );
	return { params->{section} => { params->{key} => $value } };
    } catch (YAVMM::Error $err) {
	status 500;
	return { status => "Unable to unset setting: $err" };
    }
};


# add a new key (can also set initialize the key)
post '/json/settings/:section/:key' => sub {
};

1;
